﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Text;
using System.Threading.Tasks;
using System.Web.Mvc;

namespace Sharp.Portal.Mvc.Bootstrap
{
    public static class ValidationMessageExtensions
    {
        public static ValidationMessageBuilder ValidationMessage(this BootstrapFactory factory)
        {
            return new ValidationMessageBuilder();
        }
        public static ValidationMessageBuilder ValidationMessageFor<TModel, TProperty>(this BootstrapFactory<TModel> factory,
            Expression<Func<TModel, TProperty>> expression,string name)
        {
            return factory.UnobtrusiveValidationAttributes(factory.ValidationMessage(), expression, name);
        }

        #region Private Method

        private static ValidationMessageBuilder UnobtrusiveValidationAttributes<TModel, TProperty>(
            this BootstrapFactory<TModel> factory, ValidationMessageBuilder builder, Expression<Func<TModel, TProperty>> expression, string typeName)
        {

            var name = ExpressionHelper.GetExpressionText(expression);

            if (typeName == "DropdownList")
            {
                builder.AddClass("field-validation-valid");
                builder.Attribute("data-valmsg-for", name + "label");
                builder.Attribute("data-valmsg-replace", "true");
            }
            else if (typeName == "DropdownTreeList")
            {

                builder.AddClass("field-validation-valid");
                builder.Attribute("data-valmsg-for", name + "Text");
                builder.Attribute("data-valmsg-replace", "true");
            }
            else
            {
                throw new NotImplementedException("未实现" + typeName);
            }
            //builder.Attribute("data-valmsg-for", fullHtmlFieldName);

       

            return builder;
        }


        #endregion Private Method
    }
}